package com.sxpi.chapter02.mode.factory;

import com.sxpi.chapter02.mode.IUserVipService;
import com.sxpi.chapter02.mode.NormalVipUserService;
import com.sxpi.chapter02.mode.SvipUserService;
import com.sxpi.chapter02.mode.YearvipUserService;
import com.sxpi.chapter02.pojo.User;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: 董伟豪
 * @Date: 2023/2/4 16:30
 */
public class UserVipServiceFactory {

    /**
     * 注册表的单例方式，其实也是SpringIOC的原理
     */
    static Map<String, IUserVipService> map = new ConcurrentHashMap<>();

    static {
        map.put("1",new NormalVipUserService());
        map.put("2",new YearvipUserService());
        map.put("3",new SvipUserService());
    }
    /**
     * 简单工厂设计模式
     * @param vip
     * @return
     */
    public static IUserVipService getInstance(Integer vip){

        return map.get(String.valueOf(vip));

        /*if(vip.equals(3)){
            return new SvipUserService();
        }else if(vip.equals(2)){
            return new YearvipUserService();
        }else if(vip.equals(1)){
            return new NormalVipUserService();
        }
        return null;*/
    }
}
